package first.command;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import first.daodto.DaoImpl;
import first.liter.AbstractLanguage;
import first.liter.BaseLanguage;
import first.liter.Literature;

public class AddNewLiterature implements Command {

	@Override
	public String execute(HttpServletRequest req,HttpServletResponse resp) {
		 BaseLanguage bas = null;
	     Literature literature = null;
	     try {
			req.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e3) {
			// TODO Auto-generated catch block
			e3.printStackTrace();
		}
	     resp.setCharacterEncoding("UTF-8");
	     resp.setContentType("text/html; charset=UTF-8");
	   // final PrintWriter w = resp.getWriter();
        
        final Map params = req.getParameterMap();
        
        System.out.println("\n\n\n yes i'm here!!!\n\n\n");
   
        for (Iterator paramNameIt = params.keySet().iterator(); paramNameIt.hasNext();) {
            String paramName = (String) paramNameIt.next();
                       
            if (paramName.toUpperCase().equals("LANGUAGE"))
                bas = AbstractLanguage.getLanguage(req.getParameterValues("language")[0]);            
       }
        
        for (Iterator paramNameIt = params.keySet().iterator(); paramNameIt.hasNext();) {
            String paramName = (String) paramNameIt.next();
            if (paramName.toUpperCase().equals("BKJRN_TYPE")) {
               if (req.getParameterValues("bkjrn_type")[0].toUpperCase().equals("BOOK")) 
               {
                  literature = bas.createBooks(req.getParameterValues("book_name")[0]);                  
               }

               if (req.getParameterValues("bkjrn_type")[0].toUpperCase().equals("JOURNAL")) 
               {
                    literature = bas.createJournals(req.getParameterValues("book_name")[0]);
                }
            }
        }
       
        for (Iterator paramNameIt = params.keySet().iterator(); paramNameIt.hasNext();) {
            String paramName = (String) paramNameIt.next();
            System.out.println("##"+paramName);
            if (paramName.equals("author_name")) {
                literature.setAuthor(req.getParameterValues("author_name")[0]);
            }
            if (paramName.equals("desc")) {
                literature.setDesc(req.getParameterValues("author_name")[0]);
            }
        }

        DaoImpl DaoI = new DaoImpl();

        try {
            DaoI.start();
        }
        catch (SQLException e2) {
            e2.printStackTrace();
        }

  	        int t;
  	        try
  	        {
  	        	t=DaoI.createLiterature(literature);
  	        } catch (SQLException e1)
  	        {
  	        	e1.printStackTrace();
  	        }

        List<Literature> litt = null;
        try {
            litt = DaoI.findAllLiteraturesSimple();
        }
        catch (SQLException e) {

        }

        try {
            DaoI.stop();
        }
        catch (SQLException e2) {
            e2.printStackTrace();
        }

   
        req.setAttribute("litter", litt);
		
		return "/getAllLiteratureFromDB.jspx";
	}

}
